package main

import (
	"image/color"
	"math"
)

type Star int8

func (s Star) Star(empties ...bool) string {
	if len(empties) > 0 && empties[0] {
		return starStr[s][0]
	}
	return starStr[s][1]
}

func ParseStar(b byte) (Star, bool) {
	switch b {
	case 'r', 'R', '1', 1:
		return StarRed, true
	case 'g', 'G', '2', 2:
		return StarGreen, true
	case 'y', 'Y', '3', 3:
		return StarYellow, true
	case 'b', 'B', '4', 4:
		return StarBlue, true
	case 'p', 'P', '5', 5:
		return StarPurple, true
	case 'n', 'N', '0', 0:
		return StarNone, true
	}
	return StarNone, false
}

func ParseStarFromColor(c color.Color) Star {
	// starColor 最近的颜色
	star := StarNone
	distance := int64(math.MaxInt64)
	for k, v := range starColor {
		r, g, b, _ := v.RGBA()
		r1, g1, b1, _ := c.RGBA()
		d := int64((r-r1)*(r-r1)) + int64((g-g1)*(g-g1)) + int64((b-b1)*(b-b1))
		// fmt.Printf("%s: %-12d {%d,%d,%d}, {%d,%d,%d}\n", k.Star(), d, r, g, b, r1, g1, b1)
		if d < distance {
			distance = d
			star = k
		}
	}
	return star
}

const (
	StarNone Star = iota
	StarRed
	StarGreen
	StarYellow
	StarBlue
	StarPurple
)

var starStr = map[Star][2]string{
	StarNone:   {"  ", "  "},
	StarRed:    {"\x1b[31m☆ \x1b[0m", "\x1b[31m★ \x1b[0m"},
	StarGreen:  {"\x1b[32m☆ \x1b[0m", "\x1b[32m★ \x1b[0m"},
	StarYellow: {"\x1b[33m☆ \x1b[0m", "\x1b[33m★ \x1b[0m"},
	StarBlue:   {"\x1b[34m☆ \x1b[0m", "\x1b[34m★ \x1b[0m"},
	StarPurple: {"\x1b[35m☆ \x1b[0m", "\x1b[35m★ \x1b[0m"},
}

var starColor = map[Star]color.Color{
	StarNone:   color.Black,
	StarRed:    color.RGBA{R: 240, A: 255},
	StarGreen:  color.RGBA{G: 240, A: 255},
	StarYellow: color.RGBA{R: 240, G: 240, A: 255},
	StarBlue:   color.RGBA{B: 240, A: 255},
	StarPurple: color.RGBA{R: 240, B: 240, A: 255},
}

type Point struct{ x, y int8 }
